package frobagroup.services.backstage.impl;

import frobagroup.entities.backstage.Staff;
import frobagroup.entities.backstage.StaffProfile;
import frobagroup.entities.common.Credential;
import frobagroup.repositories.backstage.StaffProfileRepository;
import frobagroup.repositories.backstage.StaffRepository;
import frobagroup.services.backstage.StaffSettingsService;
import frobagroup.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * Created by Tinsley on 2018/5/10.
 */
@Service
public class StaffSettingsServiceImpl implements StaffSettingsService {

    @Autowired
    private StaffProfileRepository staffProfileRepository;
    @Autowired
    private StaffRepository staffRepository;

    @Override
    public StaffProfile findProfileById(String sId) {

        return staffProfileRepository.findBySId(sId);
    }

    @Override
    public StaffProfile updateProfile(StaffProfile staffProfile, String sId) {

        staffProfile.setSId(sId);
        staffProfile.setUpdateTime(new Date());
        return staffProfileRepository.save(staffProfile);
    }

    @Override
    public Boolean updateStaffPassword(Credential credential, String sId) {

        Staff staff = staffRepository.findOne(sId);
        if (Objects.equals(staff.getPassword(), MD5.encode(credential.getOldPassword()))){
            staff.setPassword(MD5.encode(credential.getPassword()));
            staff.setUpdateTime(new Date());
            staffRepository.save(staff);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;

    }



}
